#ifndef _EEPROM_H_
#define _EEPROM_H_
/*private include********************************************************************/
#include "stm32l0xx_hal.h"
#include "compiler.h"
#include "error.h"
/*private define*******************************************************************/
#define EEPROM_START_ADDR   0x08080000
#define EEPROM_END_ADDR     0x080807FF
#define EEPROM_PAGE_SIZE    128
#define EEPROM_SIZE         (16 * EEPROM_PAGE_SIZE)

/*low power task eeprom address*/
#define EEPROM_BLE_ADDRESS_SIZE                         8                             /*bytes*/
#define EEPROM_BLE_ADDRESS_VALUE                        EEPROM_START_ADDR
/*sensor data eeprom address*/  

#define EEPORM_PARAMETER_SIZE                           16
#define EEPROM_PARAMETER_ADDR                           (EEPROM_START_ADDR + 8)

/*3axis*/             
#define EEPROM_3AXIS_CONFIG_PARAM_SIZE                  8                            /*bytes*/
#define EEPROM_3AXIS_CONFIG_ADDR                        (EEPROM_START_ADDR + 8)
#define EEPROM_3AXIS_FULLSCAL_ADDR                      (EEPROM_3AXIS_CONFIG_ADDR)
#define EEPROM_3AXIS_ODR_ADDR                           (EEPROM_3AXIS_CONFIG_ADDR + 2)
#define EEPROM_3AXIS_SAMPLE_WINDOW_ADDR                 (EEPROM_3AXIS_CONFIG_ADDR + 4)
#define EEPROM_3AXIS_PERIOD_ADDR                        (EEPROM_3AXIS_CONFIG_ADDR + 6)
         
/*bluenrg*/         
#define EEPROM_BLUENRG_CONFIG_PARAM_SIZE                8                            /*bytes*/
#define EEPROM_BLUENRG_CONFIG_ADDR                      (EEPROM_START_ADDR + 16)
#define EEPROM_BLUENRG_ADV_INTERVAL_ADDR                (EEPROM_BLUENRG_CONFIG_ADDR)
#define EEPROM_BLUENRG_ADV_WINDOW_ADDR                  (EEPROM_BLUENRG_CONFIG_ADDR + 2)
#define EEPROM_BLUENRG_ADV_PERIOD_ADDR                  (EEPROM_BLUENRG_CONFIG_ADDR + 4)


/*error type*/              
#define EEPROM_ERROR_TYPE_SIZE                          4   
#define EEPROM_ERROR_TYPE_ADDR                          (EEPROM_START_ADDR + 24)
         
/*time stamp*/         
#define EEPROM_DATA_TIME_STAMP_SIZE                     4
#define EEPROM_DATA_TIME_STAMP_VALUE_ADD                (EEPROM_START_ADDR + 28)
/*3axis sample */
#define EEPROM_DATA_3AXIS_SAMPLE_PERIOD_SIZE            4
#define EEPROM_DATA_3AXIS_SAMPLE_PERIOD_VALUE_ADD       (EEPROM_START_ADDR + 32)
/*voltage*/     
#define EEPROM_VOLTAGE_SIZE                             4
#define EEPROM_VOLTAGE_ADDR                             (EEPROM_START_ADDR + 36)
         
/*voltage time stamp*/         
#define EEPROM_TIME_STAMP_VOLTAGE_SIZE                  16
#define EEPROM_TIME_STAMP_VOLTAGE_VALUE_ADD             EEPROM_DATA_TIME_STAMP_VALUE_ADD
         
/*data*/           
#define EEPROM_DATA_RECORD_SIZE                         30
#define EEPROM_DATA_SIZE                                480                      /*bytes*/
#define EEPROM_DATA_BEGIN_ADDR                          (EEPROM_START_ADDR + 44)

typedef __packed struct _eeprom_data_t{
  uint16_t len;
  uint32_t address;
  uint32_t* eeprom_data;
}eeprom_data_t;

uint32_t eeprom_read_word(uint32_t address);
uint16_t eeprom_read_half_word(uint32_t address);
uint8_t eeprom_read_byte(uint32_t address);
void eeprom_write(eeprom_data_t data);
void eeprom_erase(uint32_t word_number, uint32_t address);
#endif //_EEPROM_H_
